<?php

require_once 'Zend/View/Helper/Abstract.php';

class Core_View_Helper_Interface_GetIconByFileType extends Zend_View_Helper_Abstract
{
    public $defaultIcon = 'page.png';
    public $folderIcon = 'folder.png';
    public $icons = array();

    /**
     *
     */
    public function getIconByFileType($file, $asImage = true)
    {
        $config = Zend_Registry::get('config');
        $this->icons = $config->filetypes;
        $icon = $this->getIcon($file);
        if ($asImage) {
            $base = $this->view->getBaseUrl() . '/' . $config->filepath->icons;
            return '<img src="' . $base . '/' . $icon . '" />';
        } else {
            return $icon;
        }
    }

    public function getIcon($file)
    {
        $filetype = Core_Media_Filetype::load($file);
        if ($filetype != null) {
            $type = $filetype->key;

            if (isset($this->icons->$type)) {
                $filetype = $this->icons->$type;
                return $filetype->icon;
            }
        }
        return $this->defaultIcon;
    }
}